; installer.nsi
;
; This script is based on example2.nsi and customised for ClearMerge.

;--------------------------------

; The name of the installer
Name "ClearMerge"

!define VERSION 0.3


; The file to write
OutFile "ClearMerge_${VERSION}.exe"

; The default installation directory
InstallDir $PROGRAMFILES\ClearMerge

; Registry key to check for directory (so if you install again, it will
; overwrite the old one automatically)
InstallDirRegKey HKLM "Software\CkearMerge" "Install_Dir"

; Request application privileges for Windows Vista
RequestExecutionLevel admin

; Show what's happening by default
ShowInstDetails show

; LZMA is cool
SetCompressor /SOLID /FINAL lzma


;--------------------------------

; Pages
Page license
Page directory
Page instfiles "" "" instfilesLeave

UninstPage uninstConfirm
UninstPage instfiles


;--------------------------------

LicenseText "ClearMerge is licensed under the GPLv3.  Please review this licence displayed below or check the COPYING file after installation." "Next >"
LicenseData COPYING

;--------------------------------

; The stuff to install
Section "ClearMerge (required)"

  SectionIn RO

  ; Set output path to the installation directory.
  SetOutPath $INSTDIR

  ; Put file there
  File "ClearMerge.exe"

  ; Call ClearMerge to patch the ClearCase map file
  ClearErrors
  ExecWait '"$INSTDIR\ClearMerge.exe" -install'

  IfErrors failed

  File "COPYING"
  File "README"

  ; Write the installation path into the registry
  WriteRegStr HKLM SOFTWARE\ClearMerge "Install_Dir" "$INSTDIR"

  ; Write the uninstall keys for Windows
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ClearMerge" "DisplayName" "ClearMerge"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ClearMerge" "Publisher" "Michael McTernan"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ClearMerge" "DisplayVersion" "${VERSION}"
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ClearMerge" "EstimatedSize" 123
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ClearMerge" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ClearMerge" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ClearMerge" "NoRepair" 1
  WriteUninstaller "uninstall.exe"

  Goto end

failed:
  Delete $INSTDIR\ClearMerge.exe
  SetOutPath $TEMP
  RMDir "$INSTDIR"
  Abort "Installation to the ClearCase map file to handle diffs and/or merges failed."

end:

SectionEnd


Function instfilesLeave
  IfAbort 0 +2
    MessageBox MB_OK "Installation failed.  Please ensure that both ClearCase and Araxis Merge are installed and licensed.$\r$\nThen re-run this installer to complete installation."
FunctionEnd


;--------------------------------

; Uninstaller

Section "Uninstall"

  ExecWait '"$INSTDIR\ClearMerge.exe" -uninstall'

  ; Remove registry keys
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\ClearMerge"
  DeleteRegKey HKLM SOFTWARE\ClearMerge

  ; Remove files and uninstaller
  Delete $INSTDIR\ClearMerge.exe
  Delete $INSTDIR\COPYING
  Delete $INSTDIR\uninstall.exe

  ; Remove directories used
  RMDir "$INSTDIR"

SectionEnd


; END OF FILE
